package meterMonitor.util;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

/** 
 * @ClassName: RestTemplateFactory 
 * @Description: 生成UTF-8编码的 RestTemplate
 * @author: zhenxing.li1
 * @date: 2016年9月9日 下午3:57:35  
 */
public class RestTemplateFactory {
	
	private static final String CHARSET_NAME = "UTF-8";
	
	/** 
	 * @Title: createTemplate 
	 * @Description: 创建utf-8编码的restTemplate
	 * @return: RestTemplate
	 */
	public static RestTemplate createTemplate(){
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setReadTimeout(60000);
		requestFactory.setConnectTimeout(10000);
		RestTemplate temp = new RestTemplate(requestFactory);
		List<HttpMessageConverter<?>> converters = temp.getMessageConverters();
		for (int i = 0; i < converters.size(); i++) {
			HttpMessageConverter<?> httpMessageConverter = converters.get(i);
			if (StringHttpMessageConverter.class.isAssignableFrom(httpMessageConverter.getClass())) {
				converters.set(i, new StringHttpMessageConverter(Charset.forName(CHARSET_NAME)));
				break;
			}
		}
		return temp;
	}

}
